package cn.gaozhong.org.controller;

import cn.gaozhong.basic.exception.CustomException;
import cn.gaozhong.basic.util.ExcelUtil;
import cn.gaozhong.org.domain.ShopAuditLog;
import cn.gaozhong.org.service.IShopService;
import cn.gaozhong.org.domain.Shop;
import cn.gaozhong.org.query.ShopQuery;
import cn.gaozhong.basic.util.AjaxResult;
import cn.gaozhong.basic.util.PageList;
import cn.gaozhong.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 报表
     * @return
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        List<ShopVo> shopvo = shopService.queryEcharts();
        List<Integer> states = shopvo.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> nums = shopvo.stream().map(ShopVo::getNum).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return map;
    }



    /**
     * 导入
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
       /* String file = "C:\\Users\\86176\\Desktop\\shop.xlsx";*/
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);

        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        list.forEach(System.out::println);
        //保存到数据库中[自己实现，提示：动态sql 批量插入]
        shopService.batchSave(list);
    }
    /**
     * 导出
     * @param response
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 驳回
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return AjaxResult.me();
        } catch (CustomException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 审核通过
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.me();
        } catch (CustomException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }


    /**
     * 店铺入驻
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (CustomException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }

    }


    /**
     * 保存和修改
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
     * 删除
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
     * 查看所有的员工信息
     */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
     * 分页查询数据
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
